<?php
class Entity extends Encapsulable
{	
	public function join($separator,$pattern ='?')
	{
		$str = null;
		foreach($this->toArray() as $key => $value)
		{
			if(!(is_array($value)||is_object($value)))
			{
				$mapped_value = preg_replace('#\?#',$value,$pattern);
				$str .= ($str?$separator:'') . $mapped_value;
			}
		}
		return $str;
	}
	
	public function map($str)
	{
		// get vars //
		preg_match_all('#\$[\w]+#',$str,$matches);
		if(isset($matches[0])) $vars = $matches[0]; else $vars = array();
		// define pattern for each var and replace it by the Entity corresponding value
		$pattern = array(); $replace = array();
		foreach($vars as $key => $var)
		{
			$property = substr($var,1);
			$replace[] = isset($this->$property)?$this->$property:null;
			$pattern[] = '#(\\'.$var.')#';
		}
		return preg_replace($pattern,$replace,$str);
	}
}